package com.heima.wemedia.interceptor;

import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义的拦截器
 */
@Component
@RequiredArgsConstructor
@EnableAutoConfiguration
public class WmTokenInterceptor implements HandlerInterceptor {
    /**
     * 执行目标方法之前执行的过滤器方法
     *
     * @param request
     * @param response
     * @param handler
     * @return true表示放行，false表示拦截
     * @throws Exception
     */
//    @Autowired
//    private WmUserService wmUserService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取请求头中的用户信息
        String token = request.getHeader("token");
        Integer userId = (Integer) AppJwtUtil.getClaimsBody(token).get("id");
//        WmUser wmUser = wmUserService.lambdaQuery().eq(WmUser::getId, userId).one();
        if (userId != null) {
            WmThreadLocalUtil.setUser(WmUser.builder().id(userId).build());
        }
        //4.放行
        return true;
    }

    /**
     * 完成所有的请求之后，执行的方法
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //释放资源
        WmThreadLocalUtil.clear();
    }
}
